package com.romotc.qqrobot.webqq.thread;

import org.json.JSONObject;

import com.romotc.json.tools.JsonObjectUtil;
import com.romotc.qqrobot.bean.QQLogedin;
import com.romotc.qqrobot.webqq.bean.Poll2_ResponseBody;
import com.romotc.qqrobot.webqq.qqInterfaceCall.Poll2_InterCall;
import com.romotc.qqrobot.webqq.qqInterfaceCall.dataProcessor.DateProcessor;
import com.romotc.scache.RequestSpy;



public class Poll2Thread extends Thread {

	public boolean active = true;
	
	private String qqNum;
	public Poll2Thread(String qqNum) {
		this.qqNum = qqNum;
	}
	@Override
	public void run() {
		Poll2_InterCall poll2Call = Poll2_InterCall.getInstance();
		JSONObject reqData = new JSONObject();
		QQLogedin curQq = null;
		while(active && (curQq = QQLogedin.getCurQqLogedIn(qqNum)) != null) {
			try {
				JsonObjectUtil.cleanJsonObject(reqData);
				
				poll2Call.call(curQq, reqData, new DateProcessor() {

					@Override
					public JSONObject processResponse(QQLogedin loginQQ,
							RequestSpy rqSpy, JSONObject reqDataObj)
							throws Exception {
						
						String resBody = rqSpy.getResBody();
						Poll2_ResponseBody response = null;
						try {
							response = Poll2_ResponseBody.parseFromJsonStr(resBody);
							if(response.getRetcode() == 103 || response.getRetcode() == 121) {
								//需要重新登录
								active = false;//暂时先清掉
								QQLogedin.removeQqLogedIn(qqNum);
								System.err.println("//需要重新登录@Poll2Thread");
							}
						} catch (Exception e1) {
							// TODO Auto-generated catch block
							System.out.println("Jackson 解析错误:" + resBody + " request:" + rqSpy.getURL());
							e1.printStackTrace();
							
						}
						
						//rqSpy.logMe();
						return reqDataObj;
					}
					
				});
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}	
		}

	}

	
}
